package com.mzy;

import java.awt.*;

public class StackLinked {


    public static class Node<V> {

        V value;
        Node<V> next;

        public Node(V value) {
            this.value = value;
        }
    }


    public static class MyStack<V> {
        Node<V> head;
        int size;

        public MyStack() {
            head = null;
            size = 0;
        }

        public void push(V value) {
            // 创建数据节点
            Node<V> cur = new Node<>(value);
            // 首次添加
            if (head == null) {
                head = cur;
            } else {
                cur.next = head;
                head = cur;
            }
            size++;
        }

        public V pop() {
            if (head == null || size < 1) {
                return null;
            }

            V ans = head.value;
            head = head.next;
            size--;
            return ans;
        }

        public V peek() {
            return head != null ? head.value : null;
        }
    }


    public static void main(String[] args) {

    }
}
